package com.app.gamev2.ui.splash

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.*
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.app.gamev2.MainActivity
import com.app.gamev2.R
import com.app.gamev2.ui.game.GameActivity
import com.app.gamev2.ui.theme.GameV2Theme

class SplashActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            GameV2Theme {
                SplashScreen {
                    // 跳转到游戏界面
                    startActivity(Intent(this@SplashActivity, GameActivity::class.java))
                    finish()
                }
            }
        }
    }
}

@Composable
fun SplashScreen(onStartClicked: () -> Unit) {
    // 气球浮动动画
    val floatAnimation by animateDpAsState(
        targetValue = if (true) -20.dp else 0.dp,
        animationSpec = infiniteRepeatable(
            animation = tween(1500, easing = FastOutSlowInEasing),
            repeatMode = RepeatMode.Reverse
        )
    )
    
    // 气球缩放动画
    val scaleAnimation by animateFloatAsState(
        targetValue = if (true) 1.1f else 1.0f,
        animationSpec = infiniteRepeatable(
            animation = tween(1500, easing = FastOutSlowInEasing),
            repeatMode = RepeatMode.Reverse
        )
    )
    
    // 页面可见性状态
    var isVisible by remember { mutableStateOf(true) }
    
    // 页面根布局
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0xFFE8F5FF)) // 淡蓝色背景
    ) {
        AnimatedVisibility(
            visible = isVisible,
            exit = fadeOut(animationSpec = tween(500))
        ) {
            Column(
                modifier = Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                // 气球图片
                Image(
                    painter = painterResource(id = R.drawable.character_splash),
                    contentDescription = "游戏气球",
                    modifier = Modifier
                        .padding(bottom = 50.dp)
                        .offset(y = floatAnimation)
                        .scale(scaleAnimation)
                )
                
                // 开始游戏按钮
                Button(
                    onClick = {
                        // 点击按钮后开始淡出动画
                        isVisible = false
                        // 动画结束后调用回调函数
                        onStartClicked()
                    },
                    modifier = Modifier
                        .padding(horizontal = 20.dp)
                        .height(50.dp),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(0xFF3D8BF2), // 按钮正常颜色
                        contentColor = Color.White
                    ),
                    shape = androidx.compose.foundation.shape.RoundedCornerShape(25.dp)
                ) {
                    // 按钮图标
                    Image(
                        painter = painterResource(id = R.drawable.btn_start_icon),
                        contentDescription = "开始游戏图标",
                        modifier = Modifier.size(20.dp)
                    )
                    
                    Spacer(modifier = Modifier.width(8.dp))
                    
                    // 按钮文字
                    Text(
                        text = "开始游戏",
                        fontSize = 18.sp
                    )
                }
            }
        }
    }
}